package org.distributed.transaction.core.adapter;

import org.distributed.transaction.core.context.TransactionContext;
import org.distributed.transaction.core.exception.TransactionException;
import org.distributed.transaction.core.manager.DefaultTransactionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 适配器模式
 *
 */
@Component
public class TransactionProcessorAdapter implements TransactionGroupTarget{

	@Autowired
	private DefaultTransactionManager defaultTransactionManager;
	
	/**
	 * 创建事务组
	 * @param transactionContext
	 * @throws TransactionException 
	 */
	@Override
	public void createGroup(TransactionContext transactionContext) throws TransactionException {
		defaultTransactionManager.begin(transactionContext.getGroupId());
	}
	
	/**
	 * 加入事务组
	 * @param transactionContext
	 */
	@Override
	public void joinGroup(TransactionContext transactionContext){
		try {
			defaultTransactionManager.join(transactionContext);
		} catch (Exception e) {
			
		}
		
	}
	
	/**
	 * 通知事务组
	 */
	@Override
	public void notifyGroup(TransactionContext transactionContext) {
		try {
			defaultTransactionManager.commit(transactionContext);
		} catch (Exception e) {
		}
		
	}
	
}
